local polu = fk.CreateSkill{
  name = "pegasus__polu",
}

Fk:loadTranslationTable{
  ["pegasus__polu"] = "破橹",
  [":pegasus__polu"] = "游戏开始时，或准备阶段，若场上没有【霹雳车】，将【霹雳车】置于你的宝物栏，若已有，你从弃牌堆中获得一张【杀】。",

  ["#pegasus__polu-obtain"] = "破橹：从弃牌堆中获得一张【杀】",
}

---@param room Room
---@return integer
local getCatapult = function (room)
  for _, id in ipairs(room.void) do
    if Fk:getCardById(id).name == "pegasus__catapult" then
      room:setCardMark(Fk:getCardById(id), MarkEnum.DestructOutEquip, 1)
      return id
    end
  end
  local card = room:printCard("pegasus__catapult", Card.Diamond, 9)
  room:setCardMark(card, MarkEnum.DestructOutEquip, 1)
  return card.id
end

---@param room Room
---@return boolean?
local function catapultOnfild(room)
  if table.find(room.alive_players, function (p)
    return p:getEquipment(Card.SubtypeTreasure)
    and Fk:getCardById(p:getEquipment(Card.SubtypeTreasure)).trueName == "catapult"
  end) then
    return true
  end
end

polu:addEffect(fk.EventPhaseStart, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    return player == target and player:hasSkill(polu.name) and data.phase == Player.Start
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if catapultOnfild(room) then
      local slashs = table.filter(room.discard_pile, function (id) return Fk:getCardById(id).trueName == "slash" end)
      if #slashs == 0 then return end
      local slash = room:askToChooseCard(player, {
        target = player,
        flag = { card_data = { {"pile_discard", slashs}, } },
        skill_name = polu.name,
        prompt = "#pegasus__polu-obtain"
      })
      room:obtainCard(player, slash, true, fk.ReasonJustMove, player)
    else
      room:moveCardIntoEquip(player, getCatapult(room), polu.name, true, player)
    end
  end,
})

polu:addEffect(fk.GameStart, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(polu.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if catapultOnfild(room) then
      local slashs = table.filter(room.discard_pile, function (id) return Fk:getCardById(id).trueName == "slash" end)
      if #slashs == 0 then return end
      local slash = room:askToChooseCard(player, {
        target = player,
        flag = { card_data = { {"pile_discard", slashs}, } },
        skill_name = polu.name,
        prompt = "#pegasus__polu-obtain"
      })
      room:obtainCard(player, slash, true, fk.ReasonJustMove, player)
    else
      room:moveCardIntoEquip(player, getCatapult(room), polu.name, true, player)
    end
  end
})

return polu